/**
 * Copyright © 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.test.publicconsultation.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.CrudService;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.modules.test.publicconsultation.entity.PublicConsultation;
import com.jeeplus.modules.test.publicconsultation.mapper.PublicConsultationMapper;
import com.jeeplus.modules.test.publicconsultation.entity.PublicConsultationVice;
import com.jeeplus.modules.test.publicconsultation.mapper.PublicConsultationViceMapper;

/**
 * publicconsultationService
 * @author 刘高峰
 * @version 2020-10-31
 */
@Service
@Transactional(readOnly = true)
public class PublicConsultationService extends CrudService<PublicConsultationMapper, PublicConsultation> {

	@Autowired
	private PublicConsultationViceMapper publicConsultationViceMapper;

	@Override
	public PublicConsultation get(String id) {
		PublicConsultation publicConsultation = super.get(id);
		publicConsultation.setPublicConsultationViceList(publicConsultationViceMapper.findList(new PublicConsultationVice(publicConsultation)));
		return publicConsultation;
	}

	@Override
	public List<PublicConsultation> findList(PublicConsultation publicConsultation) {
		return super.findList(publicConsultation);
	}

	@Override
	public Page<PublicConsultation> findPage(Page<PublicConsultation> page, PublicConsultation publicConsultation) {
		return super.findPage(page, publicConsultation);
	}

	@Override
	@Transactional(readOnly = false)
	public void save(PublicConsultation publicConsultation) {
		super.save(publicConsultation);
		for (PublicConsultationVice publicConsultationVice : publicConsultation.getPublicConsultationViceList()){
			if (publicConsultationVice.getId() == null){
				continue;
			}
			if (PublicConsultationVice.DEL_FLAG_NORMAL.equals(publicConsultationVice.getDelFlag())){
				if (StringUtils.isBlank(publicConsultationVice.getId())){
					publicConsultationVice.setTestDataMain(publicConsultation);
					publicConsultationVice.preInsert();
					publicConsultationViceMapper.insert(publicConsultationVice);
				}else{
					publicConsultationVice.preUpdate();
					publicConsultationViceMapper.update(publicConsultationVice);
				}
			}else{
				publicConsultationViceMapper.delete(publicConsultationVice);
			}
		}
	}

	@Override
	@Transactional(readOnly = false)
	public void delete(PublicConsultation publicConsultation) {
		super.delete(publicConsultation);
		publicConsultationViceMapper.delete(new PublicConsultationVice(publicConsultation));
	}

}